package com.lincolnXmypcqq.netty;

import com.lincoln.framework.annotation.Execute;
import com.lincoln.framework.annotation.ExecuteTypeEnum;
import com.lincoln.framework.bean.AbstractMsgNoDeviceInfo;
import com.lincoln.framework.bean.AbstractMsgNoExecute;
import com.lincoln.framework.bean.AbstractMsgNoProtocol;
import com.lincoln.framework.bean.NettyContext;
import com.lincoln.framework.interfaces.BaseProtocol;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 登陆指令{[no]|登陆|1|[IMEI]|[md5]}
 *
 * @author lincoln
 */
@Execute(deviceType = 1, protocolType = 1, executeType = ExecuteTypeEnum.LOGIN)
public class LoginExecute extends AbstractMsgNoExecute {
    private static final Logger logger = LoggerFactory.getLogger(LoginExecute.class);
    private static final int IMEI_INDEX = 1;
    private static final int DEVICE_TYPE_INDEX = 2;
    private static final int PROTOCOL_TYPE_INDEX = 3;
    /**
     * 正常长度
     */
    private static final int LENGTH = 9;
    private static final int SUCCESS = 1;
    private static final int FAIL = 0;

    @Override
    public byte[] getServerCmd() {
        return "回复登陆".getBytes();
    }

    @Override
    public byte[] getClientCmd() {
        return "登陆".getBytes();
    }

    @Override
    public Logger getLogger() {
        return logger;
    }

    @Override
    public boolean decoder(BaseProtocol baseProtocol) {
        if (baseProtocol.size() < LENGTH || baseProtocol.get(IMEI_INDEX) == null || baseProtocol.get(DEVICE_TYPE_INDEX) == null || baseProtocol.get(PROTOCOL_TYPE_INDEX) == null) {
            return false;
        }
        AbstractMsgNoDeviceInfo deviceInfo = NettyContext.getDevice();
        //设备号不为空
        if (deviceInfo.isLogin()) {
            logger.error("同一会话重复登录!");
            return false;
        }
        return true;
    }

    @Override
    public boolean execute(BaseProtocol baseProtocol) {
        AbstractMsgNoProtocol protocol = (AbstractMsgNoProtocol) baseProtocol;
        AbstractMsgNoDeviceInfo deviceInfo = NettyContext.getDevice();
        String content = new String(protocol.getPayload());
        String[] params = content.split("|");
        String deviceNo = params[IMEI_INDEX];
        Integer deviceType = Integer.parseInt(params[DEVICE_TYPE_INDEX]);
        Integer protocolType = Integer.parseInt(params[PROTOCOL_TYPE_INDEX]);
        if (null == deviceNo || "".equals(deviceNo)) {
            return false;
        }
        deviceInfo.setDeviceNo(deviceNo);
        deviceInfo.setDeviceType(deviceType);
        deviceInfo.setProtocolType(protocolType);
        boolean res = deviceInfo.login();
        AbstractMsgNoProtocol reply = new AbstractMsgNoProtocol(getServerCmd(), new byte[]{SUCCESS});
        send(reply);
        if (res) {
            logger.info(deviceNo + "登陆成功");
        }
        return res;
    }

    protected boolean send(String[] strings) {
        String content = "";
        for (String str : strings) {
            content += str + "|";
        }
        content = content.substring(0, content.length() - 1);
        try {
            AbstractMsgNoProtocol reply = new AbstractMsgNoProtocol(getServerCmd(), content.getBytes());
            send(reply);
            return true;
        } catch (Exception e) {
            logger.error("", e);
            return false;
        }
    }

    @Override
    protected boolean send() {
        return send(new String[]{String.valueOf(SUCCESS)});
    }
}
